//
// Created by Yuluoyang on 2022/4/7.
//

#include "../headers/modify.h"
//#include "ADMIN.h"

void SALES_money_printf()
{
	bool flag = false;
	for(account *p= head_user->next; p!=NULL; p=p->next)
		if(p->type=='l'||p->type=='L'){
			flag= true;
			printf("销售员:%s 的销售额为: %lf\n", p->username, p->money);}
	if(flag==false)
		printf("没有销售员目前\n");
}

void infoManager() {
    while (1) {
        print_menu("[1]管理仓库信息\t[2]管理货架信息\t[3]管理进货记录\t[4]管理销售记录\t[5]退出");
        int choice = 0;
        while (!input_int(&choice, 1, 5));
        switch (choice) {
            case 1:
                data_fruitStored_And_fruitOnShelf_modify(head_fruitStored, 1);
                break;
            case 2:
                data_fruitStored_And_fruitOnShelf_modify(head_fruitOnShelf, 2);
                break;
            case 3:
                data_purchaseMessage_modify(head_purchaseMessage);
                break;
            case 4:
                sellMessage_manager_system(head_sellMessage, head_fruitOnShelf);
                break;
            case 5:
                return;
        }
    }
}

void data_purchaseMessage_modify(purchaseMessage *head) {
    while (1) {
	    print_menu("[1]名称查找\t[2]品种查找\t[3]全部打印后进行查找\t[4]返回上一级");//11
	    int a = 0, x = 0;
	    while (!input_int(&a, 1, 4));
	    pointer *h = pointer_create();
	    switch (a) {
		    case 1:
			    printf("请输入名称：\n");
			    char keyword1[64];
			    while (!input_string(keyword1, 64));
			    bool flag = false;
			    for (purchaseMessage *p = head->next; p != NULL; p = p->next) {
				    if (strcmp(keyword1, p->name) == 0) {
					    flag = true;
					    pointer_add(h, p, 1, p->kilo);
					    x++;
				    }
			    }
			    if (flag) {
				    print_purchase(h);
				    while (1) {
					    printf("请输入你想修改或删除的序号 0-退出\n");
					    int temp = 0;
					    while (!input_int(&temp, 0, x));
					    if (temp == 0) {
						    pointer_free(h);
						    return;
					    }
					    pointer *p = h;
					    for (int i = 0; i < temp; i++)
						    p = p->next;
					    purchaseMessage_modify(p->p_purchase);
				    }
			    } else
				    printf("查无此商品进货记录！请重新查找\n");
			    break;
		    case 2:
			    printf("请输入品种：\n");
			    char keyword2[64];
			    while (!input_string(keyword2, 64));
			    bool flag2 = false;
			    for (purchaseMessage *p = head->next; p != NULL; p = p->next) {
				    if (strcmp(keyword2, p->type) == 0) {
					    flag = true;
					    pointer_add(h, p, 1, p->kilo);
					    x++;
				    }
			    }
			    if (flag2) {
				    print_purchase(h);
				    while (1) {
					    printf("请输入你想修改或删除的序号 0-退出\n");
					    int temp = 0;
					    while (!input_int(&temp, 0, x));
					    if (temp == 0) {
						    pointer_free(h);
						    return;
					    }
					    pointer *p = h;
					    for (int i = 0; i < temp; i++)
						    p = p->next;
					    purchaseMessage_modify(p->p_purchase);
				    }
			    } else
				    printf("查无此商品进货记录！请重新查找\n");
			    break;
		    case 3:
			    print_menu("[1]按进货时间输出\t[2]按照总进货额度由低到高排序\t[3]按照数量由低到高");
			    int choice = 0;
			    struct time_item t = {2000, 01, 01, 1, 1, 1};
			    while (!input_int(&choice, 1, 3));
			    for (purchaseMessage *p = head->next; p != NULL; p = p->next, x++) {
				    if (choice == 3)
					    pointer_add(h, p, 1, p->kilo);
				    if (choice == 2)
					    pointer_add(h, p, 1, p->totalPrice);
				    if (choice == 1)
					    pointer_add(h, p, 1, get_dayPassed(t, p->purchaseTime));
			    }
			    pointer_sort(h, false);
			    print_purchase(h);
			    if (h->next != NULL) {
				    while (1) {
					    printf("请输入你想修改的序号或删除的序号 0-退出\n");
					    int temp = 0;
					    while (!input_int(&temp, 0, x));
					    if (temp != 0) {
						    pointer *tp = h;
						    while (temp-- != 0)
							    tp = tp->next;
						    purchaseMessage_modify(tp->p_purchase);
					    } else if (temp == 0)
						    break;
				    }
			    } else
				    printf("没有进货记录！\n");
		    case 4:
			    pointer_free(h);
			    return;
	    }
        pointer_free(h);
    }
}

void data_sellMessage_modify(sellMessage *head) {
    while (1) {
        print_menu("[1]通过顾客名称查找\t[2]全部打印后查找\t[3]返回上一级");
        int a = 0;
        while (!input_int(&a, 1, 3));
        int x = 0;
        pointer *h = pointer_create();
        switch (a) {
            case 1:
                printf("请输入顾客姓名：\n");
                char keyword[64];
                while (!input_string(keyword, 64));
                bool flag = false;
                for (sellMessage *p = head->next; p != NULL; p = p->next) {
                    if (strcmp(keyword, p->customer) == 0) {
                        flag = true;
                        pointer_add(h, p, 2, p->totalPrice);
                        x++;
                    }
                }
                if (flag) {
                    print_sell(h);
                    while (1) {
                        printf("请输入你想修改或删除的销售记录序号 0-退出\n");
                        int temp = 0;
                        while (!input_int(&temp, 0, x));
                        if (temp == 0)
                            break;
                        pointer *p = h;
                        for (int i = 0; i < temp; i++)
                            p = p->next;
                        sellMessage_modify(p->p_sell);
                    }
                } else
                    printf("查无此商品进货记录！请重新查找\n");
                break;
            case 2:
	            print_menu("[1]按销售时间输出\t[2]按照消费额由低到高排序\t[3]按照消费件数由低到高");
	            struct time_item t = {2000, 1, 1, 0, 0, 0};
		        int temp1 = 0;
		        while (!input_int(&temp1, 1, 3))
                for (sellMessage *p = head->next; p != NULL; p = p->next, x++){
	                if (temp1 == 3)
		                pointer_add(h, p, 2, p->number);
	                if (temp1 == 2)
		                pointer_add(h, p, 2, p->totalPrice);
	                if (temp1 == 1)
		                pointer_add(h, p, 2, get_dayPassed(t, p->sellTime));
				}
		        pointer_sort(h,false);
                print_sell(h);
                if (h->next != NULL) {
                    while (1) {
                        printf("请输入你想修改或删除的序号 0-退出\n");
                        int temp = 0;
                        while (!input_int(&temp, 0, x));
                        if (temp != 0) {
                            pointer *tp = h;
                            while (temp-- != 0)
                                tp = tp->next;
                            sellMessage_modify(tp->p_sell);
                        } else
                            break;
                    }
                } else
                    printf("没有进货记录！\n");
            case 3:
	            pointer_free(h);
                return;
        }
        pointer_free(h);
    }
}

void data_fruitStored_And_fruitOnShelf_modify(fruit *head, int Id) {//Id=1
    while (1) {
	    print_menu("[1]通过名称查找\t[2]通过品种查找\t[3]全部打印后进行查找\t[4]返回上一级");//111
	    int a;
	    while (!input_int(&a, 1, 4));
	    int x = 0;
	    pointer *h = pointer_create();
	    switch (a) {
		    case 1:
			    printf("请输入名称：\n");
			    char keyword2[64];
			    while (!input_string(keyword2, 64));
			    bool flag2 = false;
			    for (fruit *p = head->next; p != NULL; p = p->next) {
				    if (strcmp(keyword2, p->name) == 0) {
					    flag2 = true;
					    pointer_add(h, p, 0, p->kilo);
					    x++;
				    }
			    }
			    if (flag2) {
				    Id == 1 ? print_fruitStored(h) : print_fruitOnShelf(h);
				    while (1) {
					    printf("请输入你想修改的序号 0-退出\n");
					    int temp = 0;
					    while (!input_int(&temp, 0, x));
					    if (temp == 0) {
						    pointer_free(h);
						    return;
					    }
					    pointer *p = h;
					    for (int i = 0; i < temp; i++)
						    p = p->next;
					    Id == 1 ? fruitStored_modify(p->p_fruit) : fruitOnShelf_modify(p->p_fruit, head_fruitOnShelf);
				    }
			    } else {
				    printf("查无此商品进货记录！请重新查找\n");
			    }
			    break;
		    case 2:
			    printf("请输入品种：\n");
			    char keyword1[64];
			    while (!input_string(keyword1, 64));
			    bool flag1 = false;
			    for (fruit *p = head->next; p != NULL; p = p->next) {
				    if (strcmp(keyword1, p->type) == 0) {
					    flag1 = true;
					    pointer_add(h, p, 0, p->kilo);
					    x++;
				    }
			    }
			    if (flag1) {
				    Id == 1 ? print_fruitStored(h) : print_fruitOnShelf(h);
				    while (1) {
					    printf("请输入你想修改的序号 0-退出\n");
					    int temp = 0;
					    while (!input_int(&temp, 0, x));
					    if (temp == 0) {
						    pointer_free(h);
						    return;
					    }
					    pointer *p = h;
					    for (int i = 0; i < temp; i++)
						    p = p->next;
					    Id == 1 ? fruitStored_modify(p->p_fruit) : fruitOnShelf_modify(p->p_fruit, head_fruitOnShelf);
				    }
			    } else {
				    printf("查无此商品进货记录！请重新查找\n");
			    }
			    break;
		    case 3:
			    print_menu("[1]按到期时间输出\t[2]按照单价由低到高排序\t[3]按照数量由低到高");
			    struct time_item t = {2000, 1, 1, 0, 0, 0};
			    int temp1 = 0;
			    while (!input_int(&temp1, 1, 3));
			    for (fruit *p = head->next; p != NULL; p = p->next, x++) {
				    if (temp1 == 3)
					    pointer_add(h, p, 0, p->kilo);
				    if (temp1 == 2)
					    pointer_add(h, p, 0, p->unitPrice);
				    if (temp1 == 1)
					    pointer_add(h, p, 0, get_dayPassed(t, p->life));
			    }
			    pointer_sort(h, false);
			    Id == 1 ? print_fruitStored(h) : print_fruitOnShelf(h);
			    if (h->next != NULL) {
				    while (1) {
					    printf("请输入你想修改的序号 0-返回上一级\n");
					    int temp = 0;
					    while (!input_int(&temp, 0, x));
					    if (temp != 0) {
						    pointer *tp = h;
						    while (temp-- != 0) tp = tp->next;
						    Id == 1 ? fruitStored_modify(tp->p_fruit) : fruitOnShelf_modify(tp->p_fruit,
						                                                                    head_fruitOnShelf);
					    } else
						    break;
				    }
			    } else
				    printf("没有进货记录！\n");
			    break;
		    case 4:
			    pointer_free(h);
			    return;
	    }
        pointer_free(h);
    }
}

void purchaseMessage_modify(purchaseMessage *target) {
	int choice = 0;
	print_menu("[1]修改单项信息\t[2]修改多项信息\t[3]删除\t[4]退出");//1111
	while (!input_int(&choice, 1, 4));
	if (choice == 4) return;
	bool isALl = choice == 2;
	if (choice == 3) {
		print_menu("[1]确认删除\t[2]放弃了");
		int what = 0;
		while (!input_int(&what, 1, 2));
		if (what == 1)
			purchaseMessage_del(head_purchaseMessage, target);
		return;
	}
	if (!isALl) {
		printf("请选择修改的信息\n");
		print_menu("[1]名称\t[2]品种\t[3]单价\t[4]数量\t[5]进货商\t[6]进货时间\t[7]退出\n");
		while (!input_int(&choice, 1, 7));
	} else choice = 0;
	if (choice == 7)return;
	puts("开始修改……Enter可跳过修改项");
	switch (choice) {
		case 0:
		case 1:
			printf("请输入新的名称：");
			input_string(target->name, 64);
			if (!isALl) break;
		case 2:
			printf("请输入新的品种：");
			input_string(target->type, 64);
			if (!isALl) break;
		case 3:
			printf("请输入新的单价 自动计算总价\n");
			input_double(&target->unitPrice);
			target->totalPrice = target->kilo * target->unitPrice;
			if (!isALl) break;
		case 4:
			printf("请输入新的数量 自动计算总价\n");
			input_double(&target->kilo);
			target->totalPrice = target->kilo * target->unitPrice;
			if (!isALl) break;
		case 5:
			printf("请输入新的进货商\n");
			input_string(target->supplier, 64);
			if (!isALl) break;
		case 6:
			printf("请输入新的进货时间 格式(yyyy/mm/dd hh:mm:ss)\n");
			while (1) {
				char t = getchar();
				if (t == '\n')
					break;
				if (scanf("%d/%d/%d %d:%d:%d", &target->purchaseTime.year,
				          &target->purchaseTime.month,
				          &target->purchaseTime.day,
				          &target->purchaseTime.hour, &target->purchaseTime.minute,
				          &target->purchaseTime.second) == 6) {
					target->purchaseTime.year += (t - '0') * 1000;
					while (getchar() != '\n');
					break;
				} else {
					while (getchar() != '\n');
					puts("输入错误 请重新输入：\n");
				}
			}
			break;
	}
	printf("本次修改结束\n");
}

void fruitStored_modify(fruit *target) {
	int choice;
	print_menu("[1]修改单项\t[2]修改多项\t[3]删除\t[4]退出");//111
	while (!input_int(&choice, 1, 4));
	if (choice == 4) return;
	bool isAll = choice == 2;
	if (choice == 3) {
		print_menu("[1]确认删除\t[2]后悔了");//11
		while (!input_int(&choice, 1, 2));
		if (choice == 1) {
			fruit_del(head_fruitStored, target);
			puts("本次修改结束");
		}
		return;
	}
	if (!isAll) {
		print_menu("[1]名称\t[2]品种\t[3]单价\t[4]数量\t[5]保质期\t[6]到期时间\t[7]退出");
		while (!input_int(&choice, 1, 7));
	} else choice = 0;
	if (choice == 7)return;
	puts("开始修改……Enter可跳过修改项");
	switch (choice) {
		case 0:
		case 1:
			printf("请输入新的名称\n");
			input_string(target->name, 64);
			if (!isAll) break;
		case 2:
			printf("请输入新的品种\n");
			input_string(target->type, 64);
			if (!isAll) break;
		case 3:
			printf("请输入新的单价\n");
			input_double(&target->unitPrice);
			if (!isAll) break;
		case 4:
			printf("请输入新的数量\n ");
			input_double(&target->kilo);
			if (!isAll) break;
		case 5:
			printf("请输入新的保质期\n ");
			input_int(&target->lifetime, 0, 1000);
			if (!isAll) break;
		case 6:
			printf("请输入新的到期时间 时间格式为yyyy/mm/dd\n");
			while (1) {
				char t = getchar();
				if (t == '\n')
					break;
				if (scanf("%d/%d/%d", &target->life.year,
				          &target->life.month,
				          &target->life.day) == 3) {
					target->life.year += (t - '0') * 1000;
					while (getchar() != '\n');
					break;
				} else {
					while (getchar() != '\n');
					puts("输入错误 请重新输入：\n");
				}
			}
			break;
		case 7:
			return;
	}
	printf("本次修改结束\n");
}

void fruitOnShelf_modify(fruit *target, fruit *head) {
    int choice = 0;
    print_menu("[1]修改单项\t[2]修改多项\t[3]删除\t[4]退出");
    while (!input_int(&choice, 1, 4));

    bool isAll = choice == 2;
    if (choice == 3) {
        print_menu("[1]确认删除\t[2]后悔了");
        while (!input_int(&choice, 1, 2));
        if (choice == 1) {
            fruit_del(head, target);
        }
        return;
    }
    if (choice == 4) return;

    if (!isAll) {
	    print_menu("[1]名称\t[2]品种\t[3]单价\t[4]数量\t[5]价格模式\t[6]等级\t[7]到期时间\t[8]上架时间\t[9]取消修改\n");
	    while (!input_int(&choice, 1, 9));
    } else choice = 0;
    puts("开始修改……Enter可跳过修改项");
    switch (choice) {
	    case 0:
	    case 1:
		    printf("请输入新的名称：");
		    input_string(target->name, 64);
		    if (!isAll) break;
	    case 2:
		    printf("请输入新的品种：");
		    input_string(target->type, 64);
		    if (!isAll) break;
	    case 3:
		    printf("请输入新的单价 \n");
		    input_double(&target->unitPrice);
		    if (!isAll) break;
	    case 4:
		    printf("请输入新的数量 \n");
		    input_double(&target->kilo);
		    if (!isAll) break;
	    case 5:
		    printf("请输入新的价格模式\n");
		    if (input_int(&choice, 1, 3))
			    target->mode = choice - 1;
		    if (!isAll) break;
	    case 6:
		    printf("请输入新的等级 \n");
		    if (input_int(&choice, 1, 3))
			    target->level = choice - 1;
		    if (!isAll) break;
	    case 7:
		    printf("请输入新的上架时间 上架时间格式(YYYY/MM/DD hh:mm:ss)\n");
		    while (1) {
			    char t = getchar();
			    if (t == '\n')
				    break;
			    if (scanf("%d/%d/%d %d:%d:%d", &target->shelfTime.year,
			              &target->shelfTime.month,
			              &target->shelfTime.day,
			              &target->shelfTime.hour, &target->shelfTime.minute,
			              &target->shelfTime.second) == 6) {
				    target->shelfTime.year += (t - '0') * 1000;
				    while (getchar() != '\n');
				    break;
			    } else {
				    while (getchar() != '\n');
				    puts("输入错误 请重新输入：\n");
			    }
		    }
		    if (!isAll) break;
	    case 8:
		    printf("请输入新的到期时间 到期时间格式(YYYY/MM/DD)\n");
		    while (1) {
			    char t = getchar();
			    if (t == '\n')
				    break;
			    if (scanf("%d/%d/%d", &target->life.year,
			              &target->life.month,
			              &target->life.day) == 3) {
				    target->life.year += (t - '0') * 1000;
				    while (getchar() != '\n');
				    break;
			    } else {
				    while (getchar() != '\n');
				    puts("输入错误 请重新输入：\n");
			    }
		    }
		    break;
	    case 9:
		    return;
    }
	printf("本次修改结束\n");
}

void sellMessage_modify(sellMessage *target) {
    print_menu("[1]修改单项\t[2]修改多项\t[3]删除\t[4]退出");//111
    int choice = 0;
    while (!input_int(&choice, 1, 4));
    bool isAll = choice == 2;
    if (choice == 3) {
        sellMessage_del(head_sellMessage, target);
        return;
    }
    if (choice == 4) return;

    if (!isAll) {
        printf("请选择修改的信息\n");
        print_menu("[1]顾客信息\t[2]顾客总消费\t[3]顾客所购买的商品信息\t[4]销售时间\t[5]取消修改");
        while (!input_int(&choice, 1, 6));
    } else choice = 0;
	if(choice==5)return;
    puts("开始修改……Enter可跳过修改项");
    switch (choice) {
	    case 0:
	    case 1:
		    printf("请输入新的顾客信息\n");
		    input_string(target->customer, 64);
		    if (!isAll) break;
	    case 2:
		    printf("请输入新的顾客总消费 \n");
		    double key = 0;
		    target->totalPrice = 0;
		    for (fruit *p = target->head->next; p != NULL; p = p->next)
			    key = p->unitPrice * p->kilo + target->totalPrice;
		    input_double(&target->totalPrice);
		    if (key != target->totalPrice) {
			    printf("输入的总消费和顾客购买商品的总消费有冲突，是否确认修改？");
			    int temp = 0;
			    print_menu("[1]确认\t[2]放弃");
			    while (! input_int(&temp, 1, 2));
			    if (temp == 2)
				    target->totalPrice = key;
		    }
		    if (!isAll) break;
	    case 3: {
		    printf("请输入新的顾客所购买的商品信息\n");
		    pointer *h = pointer_create();
		    for (fruit *q = target->head->next; q != NULL; q = q->next)
			    pointer_add(h, q, 0, target->head->kilo);
		    printf("下面为你选择的要修改的项\n");
		    print_fruitOnShelf(h);
		    printf("请选择你要修改的项\n");
		    if (!input_int(&choice, 1, target->number)) break;
		    fruit *temp = target->head;
		    while (choice--) temp = temp->next;
		    fruitOnShelf_modify(temp, target->head);  //调用修改函数
		    target->number = 0;
		    for (fruit *q = target->head->next; q != NULL; q = q->next, target->number++); //重新矫正购买件数
		    pointer_free(h);
		    target->totalPrice = 0;
		    for (fruit *p = target->head->next; p != NULL; p = p->next)
			    target->totalPrice = p->unitPrice * p->kilo + target->totalPrice;
		    if (!isAll) break;
	    }
	    case 4:
		    printf("请输入新的销售时间 时间格式为 YYYY/MM/DD hh:mm:ss\n");
		    while (1) {
			    char t = getchar();
			    if (t == '\n')
				    break;
			    if (scanf("%d/%d/%d %d:%d:%d", &target->sellTime.year,
			              &target->sellTime.month,
			              &target->sellTime.day,
                          &target->sellTime.hour, &target->sellTime.minute,
                          &target->sellTime.second) == 6) {
                    target->sellTime.year += (t - '0') * 1000;
                    while (getchar() != '\n');
                    break;
                } else {
                    while (getchar() != '\n');
                    puts("输入错误 请重新输入：\n");
                }
            }
            break;
    }
	printf("本次修改结束\n");
}
